<?php
/*
*
* File: Request.php
* @author: Rams
*
*
*/

require_once("Net/Whois.php");
require_once("Net/Geo.php");

//Class Request
class Request {
	
	//Domain Name
	var $domain;

	//IP Address
	var $ip;

	//Domain Raw Statistics
	var $raw_stats;

	//Domain Statistics
	var $stats;

	//Domain Raw Statistics
	var $sections;

	//Whois Object
	var $who;

	//Whois Object
	var $geo;


	//Constructor
	function Request($domain) {

		$this->domain = $domain;

		$this->who = new Net_Whois($this->domain);

		$this->geo = new Net_Geo();
	}

	//Constructor
	function get_stats() {

		if ( empty($this->raw_stats) ) {

			$this->raw_stats = $this->who->query($this->domain);
		}
	}

	//Process Statistics
	function process_stats() {

		$this->get_stats();

		if ( empty($this->stats) ) {

			$this->sections = array();
			$c = 0;
			$lines = explode("\n", $this->raw_stats);
			
			//###################################
			foreach( $lines as $line ) {
				
				$line = trim($line);

				if (empty($line) ) {
					$c++;
					continue;
				}

				$this->sections[$c][]= $line;
			}

			//####################################
			foreach( $this->sections as $section ) {

				foreach ($section as $line) {
				
					$temp = explode(":", $line);

					if (count($temp) == 2 ) {

						$key = strtolower(trim($temp[0]));

						$this->stats[$key] = trim($temp[1]);
					}
				} //For loop
			} //For loop
			//#####################################

		}
	}

	//Get IP address
	function get_ip() {

		if (empty($this->ip)) {

			$this->ip = gethostbyname($this->domain);
		}

		return $this->ip;
	}

	//Get Country
	function get_country() {

		$this->get_ip();

		return $this->geo->getCountry($this->ip);
	}

	//Get Age
	function get_age() {

		$this->process_stats();
		//echo "<pre>";print_r($this->stats);

		if (isset($this->stats['creation date'])) {

			$age = $this->calc_age($this->stats['creation date']);
			return floor($age/365) . " Yrs. " . ($age % 365) . " Days";
		} else {

			return 'Unable to Determine';
		}
	}

	//Get Age
	function calc_age($date) {

		$months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

		$temp = explode("-",$date);
		$month = array_search($temp[1], $months);
		$month = $month+1;

		$current = mktime();

		//echo $month,'-',intval($temp[0]),'-',intval($temp[1]);
		$timestamp = mktime(0,0,0,$month,intval($temp[0]),intval($temp[2]));

		$age = ( $current - $timestamp ) / (60 * 60 * 24);

		return $age;
	}

	//Alexa Traffic Rank
	//Avg. Load Time

	//
	function get_whois() {

		$this->process_stats();
		$out = '';

		foreach ($this->sections as $key => $stats) {

			if ($key >= 8) {

				$out .= '<p>';

				for ($i=0; $i<count($stats);$i++) {

					if ($i == 0 && strpos($stats[$i], ":") !== false ) {
						$out .= '<b>'.$stats[$i].'</b><br>';
					} else {
						$out .= ''.$stats[$i].'<br>';
					}
				}
				$out .= '</p>';
			}
		}
		return $out;
	}

	//
}
?>